package top.i51213.fastlink;

import android.content.Context;
import android.content.Intent;

import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public  class Contract <I,O> extends ActivityResultContract<I,O>{
    @FunctionalInterface
    public interface CreateIntent<I>{
        Intent invoke(Context context, I input);
    }
    @FunctionalInterface
    public interface ParseResult<O>{
        O invoke(int resultCode, Intent intent);
    }
    private CreateIntent<I> createIntent;
    private ParseResult<O> parseResult;

    @NonNull
    @Override
    public Intent createIntent(@NonNull Context context, I input) {
        return createIntent.invoke(context,input);
    }

    @Override
    public O parseResult(int resultCode, @Nullable Intent intent) {
        return parseResult.invoke(resultCode,intent);
    }

    public static class Builder<I,O>{
        private final Contract<I,O> contract=new Contract<I,O>();
        public Builder(){
        }
        public Builder<I,O> setCreateIntent(CreateIntent<I> createIntent){
            contract.createIntent=createIntent;
            return this;
        }
        public Builder<I,O> setParseResult(ParseResult<O> parseResult){
            contract.parseResult=parseResult;
            return this;
        }
        public Contract<I,O> build(){
           return contract;
        }
    }
}
